<template>
    <div class="edit">
        <div class="editTitle">复制销售订单</div>
        <el-form :model="form" ref="form" label-width="100px" size="mini">
            <el-form-item label="单据编号">
                <el-input v-model="form.code" placeholder="请输入，忽略将自动生成" class="code"></el-input>
            </el-form-item>
        </el-form>
        <div class="line"></div>

        <!-- 产品明细 -->
        <sales-Detail @productTableList='productTableList' @getDelId='getDelId' ref='detail'></sales-Detail>

        <div class="btn">
            <el-button type="primary" plain size="small" @click="$router.push('/productionManagement/salesOrder')">取消
            </el-button>
            <el-button type="primary" size="small" @click="handleProduct">保存</el-button>
        </div>
    </div>
</template>

<script>
    import {
        formDetail,
        editSalesOrder
    } from '@/api/produceController'
    import salesDetail from './salesDetail.vue'
    import formLibrary from '@/components/formLibrary'
    export default {
        data() {
            return {
                form: {
                    code: '',
                    salesOrderProductVo:[]
                },
                list:[]
            }
        },
        components: {
            formLibrary,
            salesDetail
        },
        mounted() {
            this.getFormDetail({
                id: this.$route.query.id
            })
        },
        methods: {
            //详情
            getFormDetail(data) {
                formDetail(data).then(res => {
                    if (res.code == 200) {
                        let productTable = []
                        this.form.code = res.data.code
                        productTable = res.data.formTableDetails.map(i =>{
                            return {
                                id:i.id,
                                productId:i.productId,
                                productCode:i.productCode,
                                productName:i.productName,
                                specification:i.specification || '-',
                                workOrderCode:i.workOrderCode,
                                handleNumber:i.handleNumber,
                                finishNumber:i.finishNum,
                                time:[i.startTime,i.endTime],
                                customFieldVos:i.customFieldVos,
                                workOrderId:i.workOrderId,
                                salesId:res.data.id,
                                salesCode:i.ftCode,
                                workOrderState:i.state,
                            }
                        })
                        //走导入数据的逻辑
                        this.$refs.detail.getProductTable({
                            title: '导入',
                            from: 'fromEdit',
                            productTable: productTable
                        })
                    }
                })
            },

            //销售订单
            getEditSalesOrder(data) {
                editSalesOrder(data).then(res => {
                    if (res.code == 200) {
                        this.$router.push('/productionManagement/salesOrder')
                        this.utils.showMsg('复制成功', 'success')
                    }
                })
            },

            //明细列表数据
            productTableList(list) {
                this.list = list
            },
            //列表删除id
            getDelId(idArr){
                this.form.delDetailIds = idArr
            },
            //保存
            handleProduct() {
                const {formatTime} = this.utils
                if (!this.list.length) {
                    this.utils.showMsg('自定义表单产品不能为空', 'error')
                    return
                }
                this.form.salesOrderProductVo = []
                this.list.forEach(i => {
                    this.form.salesOrderProductVo.push({
                        productId:i.productId,
                        handleNumber:i.handleNumber,
                        startTime:formatTime(i.time[0],'yyyy-MM-dd hh:mm:00'),
                        endTime:formatTime(i.time[1],'yyyy-MM-dd hh:mm:59'),
                        customFieldVos:i.customFieldVos
                    })
                })
                this.form.salesOrderProductVo.forEach(i => {
                    i.customFieldVos = i.customFieldVos.map(k => {
                        return {
                            customFieldId:k.customFieldId,
                            value:k.value
                        }
                    })
                })
                this.getEditSalesOrder(this.form)
            },
        },
    }
</script>

<style lang="less" scoped>
    .edit {
        padding: 20px;
        overflow: auto;

        code {
            font-family: sans-serif;
        }

        .editTitle {
            font-size: 16px;
            color: #1f273d;
            padding-bottom: 20px;
        }

        .line {
            margin: 28px 0 24px 0;
            border-bottom: 1px solid #e8e8e8;
        }

        .btn {
            margin: 26px 0;
            text-align: center;

            /deep/ .el-button--primary span {
                margin-left: 0;
            }

            /deep/ .el-button {
                padding: 7px 35px;
            }
        }

        /deep/ .el-input.code {
            width: 300px;
        }
    }
</style>